const fp = require("fastify-plugin");
const { DataTypes } = require("sequelize");
const sequelize = require("../sequelize");
const path = require("path");
async function plugin(fastify, options) {
  // const scriptName = path.basename(__filename);
  // let name = scriptName.split(".")[0];
  let database = fastify.sequelize.define("table", {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      comment: "自增",
    },
    uuid: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: "唯一ID",
    },
    username: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: "",
      comment: "用户名",
    },
    password: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: "密码",
    },
    nickname: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "昵称",
    },
    phone: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: "",
      comment: "手机号",
    },
    weixin: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: "",
      comment: "微信号",
    },
    email: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "邮箱",
    },
    avatar: {
      type: DataTypes.STRING(200),
      allowNull: false,
      defaultValue: "",
      comment: "头像",
    },
    merchant_name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      defaultValue: "",
      comment: "商户名称",
    },
    merchant_type: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "商户类型",
    },
    merchant_code: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "商户代号",
    },
    merchant_score: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "商户积分",
    },
    business_license: {
      type: DataTypes.STRING(2000),
      allowNull: false,
      defaultValue: "",
      comment: "营业执照",
    },
    merchant_logo: {
      type: DataTypes.STRING(500),
      allowNull: false,
      defaultValue: "",
      comment: "商铺商标",
    },
    merchant_longitude: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: "",
      comment: "经度",
    },
    merchant_latitude: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: "",
      comment: "纬度",
    },
    merchant_province: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "省",
    },
    merchant_city: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "市",
    },
    merchant_region: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: "",
      comment: "区",
    },
    merchant_address: {
      type: DataTypes.STRING(1000),
      allowNull: false,
      defaultValue: "",
      comment: "商铺地址",
    },
    audit_state: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: "审核状态（0:未审核,1:审核中,2:已通过,3:已拒绝）",
    },
    role_code: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: "角色码（1:用户,2:商户,4:客服,8:普通理员,16:超级管理员）",
      validate: {
        isIn: [[1, 2, 4, 8, 16]],
      },
    },
    state: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 0,
      comment: "状态",
    },
  });
  database.sync();
}

module.exports = fp(plugin);
